// pages/showlist/showlist.js
const app = getApp()
wx.cloud.init({
    env: 'cloud1-8gbitjqz4bc93039',
})
const db = wx.cloud.database()
Page({
    /**
     * 页面的初始数据
     */
    data: {
        list:[],
        curpage:1,
        pages:0,
        prevc:"gray",
        nextc:"black",
		pagesize:10000,
        idx:0,
        inId:0
    },
    add()
    {
        wx.navigateTo({
          url: 'addnotice/addnotice',
        })
    },
    showDetail:function(e)
    {
        console.log(e);
        let cur=e.currentTarget.dataset.item;
        console.log(cur);
        wx.setStorageSync('showThisNotice', cur);
        wx.navigateTo({
          url: 'sShownotice/sShownotice',
        });
    },
    async getFullList()
    {
        const MAX_LIMIT = 20
        const countResult= await db.collection('notice')
            .where({
                class:app.globalData.sList[0].class
            }).count()
        console.log(app.globalData.tInfo.name)
        console.log(countResult)
        const total = countResult.total
        const times = Math.ceil(total / MAX_LIMIT)
        var tmpList=[]
        for (let i = 0; i < times; i++) {
            await db.collection('notice')
                .skip(i * MAX_LIMIT)
                .limit(MAX_LIMIT)
                .where({
                    class:app.globalData.sList[0].class
                })
                .get().then(res => {
                    for (let j = 0; j < res.data.length; j++) {
                        tmpList.push(res.data[j])
                    }

                })
        }
        var ttmp=[]
        for(var i=0;i<tmpList.length;i++)
        {
            if(tmpList[i].studentList.split(' ').findIndex((item, index, arr) => {
                return item == app.globalData.sList[0].name
            }) != -1)
            {
                ttmp.push(tmpList[i])
            }
        }
        ttmp.reverse()
        this.setData({
            fullList:ttmp
        })
        console.log(this.data.fullList)
        this.setData({
            pages:Math.ceil(this.data.fullList.length/this.data.pagesize)
        })
        if(this.data.pages==1)
        {
            this.setData({
                nextc:"grey"
            })
        }
    },
    async getList()
    {
        var curlist=[]
        for(var i=(this.data.curpage-1)*this.data.pagesize;i<Math.min((this.data.curpage)*this.data.pagesize,this.data.fullList.length);i++)
        {
            curlist.push(this.data.fullList[i])
        }
        this.setData({
            list:curlist,
            cursize:curlist.length
        })
        console.log(this.data.list)
    },
	aniStart:function()
	{
        clearInterval(this.data.inId)
		this.setData({
			idx:-100
		})
		let that=this
		var id=setInterval(function(){
			that.aniNext()
        },60)
        console.log(id)
        this.setData({
            inId:id
        })
	},
	aniNext:function()
	{
		var idxx=this.data.idx
		idxx++;
		console.log(idxx)
		this.setData({
			idx:idxx
        })
        console.log(this.data.idx>this.data.cursize)
		if(this.data.idx>this.data.cursize)
		{
			clearInterval(this.data.inId)
		}
	},
    goPrev:function()
    {
        if(this.data.curpage==1)
            return;
        var cp=this.data.curpage-1;
        this.setData({
            nextc:"black",
            curpage:cp
        })
        if(this.data.curpage==1)
        {
            this.setData({
                prevc:"gray"
            })
		}
		this.getList();
		this.getcolor();
    },
    goNext:function()
    {
        if(this.data.curpage==this.data.pages)
            return;
        var cp=this.data.curpage+1;
        this.setData({
            prevc:"black",
            curpage:cp
        })
        if(this.data.curpage==this.data.pages)
        {
            this.setData({
                nextc:"gray"
            })
		}
		this.getList();
		this.getcolor();
    },
    async init()
    {
        await this.getFullList()
        await this.getList()
        this.setData({
            idx:-1
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
		this.setData({
			curpage:1
        })
        this.init()
        this.aniStart()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.init()
        this.aniStart()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})